import java.util.Scanner;

public class MaxSum {
    public static int findMaxSubarraySum(int[] nums) {
        // 处理空数组情况
        if (nums == null || nums.length == 0) {
            throw new IllegalArgumentException("数组不能为空");
        }

        int currentMax = nums[0];
        int globalMax = nums[0];

        for (int i = 1; i < nums.length; i++) {
            currentMax = Math.max(nums[i], currentMax + nums[i]);
            globalMax = Math.max(globalMax, currentMax);
        }

        return globalMax;
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("请输入数组的元素个数: ");
        int n = scanner.nextInt();

        int[] nums = new int[n];
        System.out.println("请输入" + n + "个整数，用空格分隔:");
        for (int i = 0; i < n; i++) {
            nums[i] = scanner.nextInt();
        }

        int maxSum = findMaxSubarraySum(nums);
        System.out.println("子数组之和的最大值为: " + maxSum);

        scanner.close();
    }
}
